//
//  MeController.m
//  youthNote
//
//  Created by huangrensheng on 15/12/29.
//  Copyright © 2015年 laber. All rights reserved.
//

#import "MeController.h"
#import "LoginController.h"
#import "MeUserInfoCell.h"
#import "MeCommonCell.h"
#import "MeMyPostListController.h"

@interface MeController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView *meTable;
@property (nonatomic , strong) NSDictionary *userInfo;
@property (nonatomic , strong) NSArray *titleListArray;
@end

@implementation MeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createWithLeftStyle:nil middleStyle:@{@"type":@"title",@"title":@"我"} rightStyle:nil];
    
    self.meTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.meTable.backgroundColor = BACK_COLOR;
    self.meTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.meTable.delegate = self;
    self.meTable.dataSource = self;
    [self.view addSubview:self.meTable];
    [self.meTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.titleListArray = [NSArray arrayWithObjects:@"我相关的帖子",@"我关注的资讯 ",@"我关注的社区",@"我关注的话题",@"我关注的人", nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (!self.userInfo) {
        [self getUserInfo];
    }
}

- (void)getUserInfo{
    [[HttpManger shareHttpManage] postWithUrl:getUserInfo params:nil success:^(NSDictionary *data) {
        self.userInfo = data;
        NSLog(@"data");
        [self.meTable reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return self.titleListArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 74;
    }
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 1 && self.userInfo != nil) {
        UIView *view = [[UIView alloc] init];
        UIButton *btn = [[UIButton alloc] initWithTitle:@"退出" font:[UIFont boldSystemFontOfSize:18] coclor:[UIColor whiteColor]];
        [btn addTarget:self action:@selector(loginOut) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(view).insets(UIEdgeInsetsMake(20, 15, 10, 15));
        }];
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0 && indexPath.section == 0){
        return 210;
    }
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *identifer = @"MeUserInfoCell";
        MeUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[MeUserInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        cell.cellData = self.userInfo;
        return cell;
    }else{
        static NSString *identifer = @"MeCommonCell";
        MeCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[MeCommonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        cell.commonLabel.text = self.titleListArray[indexPath.row];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && self.userInfo == nil) {
        LoginController *login = [[LoginController alloc] init];
        login.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:login animated:YES];
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            MeMyPostListController *myPost = [[MeMyPostListController alloc] init];
            myPost.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:myPost animated:YES];
        }
    }
}

- (void)loginOut{
    [[HttpManger shareHttpManage] postWithUrl:loginOut params:nil success:^(NSDictionary *data) {
        self.userInfo = nil;
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
        [self.meTable reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
